package cn.dglydrpy.fundmanagement.service.impl;

import cn.dglydrpy.fundmanagement.dao.entiey.RiskAssessment;
import cn.dglydrpy.fundmanagement.dao.mapper.RiskAssessmentMapper;
import cn.dglydrpy.fundmanagement.service.IRiskAssessmentService;
import cn.dglydrpy.fundmanagement.service.dto.RiskAssessmentDto;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RiskAssessmentService implements IRiskAssessmentService {

    @Autowired
    private RiskAssessmentMapper riskAssessmentMapper;

    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public int saveRiskAssessment(RiskAssessmentDto riskAssessmentDto) {
        RiskAssessment riskAssessment = new RiskAssessment();
        BeanUtils.copyProperties(riskAssessmentDto, riskAssessment);
        riskAssessment.setCreatedTime(sdf.format(new Date()));
        riskAssessment.setUpdatedTime(sdf.format(new Date()));
        if (riskAssessment.getStatus() == null || riskAssessment.getStatus().isEmpty()) {
            riskAssessment.setStatus("1"); // 1-有效 0-无效
        }
        return riskAssessmentMapper.insert(riskAssessment);
    }

    @Override
    public void updateRiskAssessment(RiskAssessmentDto riskAssessmentDto) {
        RiskAssessment riskAssessment = new RiskAssessment();
        BeanUtils.copyProperties(riskAssessmentDto, riskAssessment);
        riskAssessment.setUpdatedTime(sdf.format(new Date()));
        riskAssessmentMapper.updateById(riskAssessment);
    }

    @Override
    public void deleteRiskAssessment(Long id) {
        riskAssessmentMapper.deleteById(id);
    }

    @Override
    public RiskAssessmentDto getRiskAssessmentById(Long id) {
        RiskAssessment riskAssessment = riskAssessmentMapper.selectById(id);
        if (riskAssessment == null) {
            return null;
        }
        RiskAssessmentDto riskAssessmentDto = new RiskAssessmentDto();
        BeanUtils.copyProperties(riskAssessment, riskAssessmentDto);
        setRiskLevelName(riskAssessmentDto);
        return riskAssessmentDto;
    }

    @Override
    public Page<RiskAssessmentDto> getRiskAssessmentsByConditions(String fundCode, String portfolioId, String riskLevel, int pageNo, int pageSize) {
        Page<RiskAssessment> page = new Page<>(pageNo, pageSize);
        QueryWrapper<RiskAssessment> queryWrapper = new QueryWrapper<>();
        if (fundCode != null && !fundCode.isEmpty()) {
            queryWrapper.like("fund_code", fundCode);
        }
        if (portfolioId != null && !portfolioId.isEmpty()) {
            queryWrapper.eq("portfolio_id", portfolioId);
        }
        if (riskLevel != null && !riskLevel.isEmpty()) {
            queryWrapper.eq("risk_level", riskLevel);
        }
        queryWrapper.eq("status", "1");
        queryWrapper.orderByDesc("created_time");
        riskAssessmentMapper.selectPage(page, queryWrapper);

        Page<RiskAssessmentDto> dtoPage = new Page<>(pageNo, pageSize, page.getTotal());
        List<RiskAssessmentDto> records = page.getRecords().stream().map(riskAssessment -> {
            RiskAssessmentDto dto = new RiskAssessmentDto();
            BeanUtils.copyProperties(riskAssessment, dto);
            setRiskLevelName(dto);
            return dto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(records);
        return dtoPage;
    }

    @Override
    public List<RiskAssessmentDto> getAllRiskAssessments() {
        QueryWrapper<RiskAssessment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "1");
        queryWrapper.orderByDesc("created_time");
        List<RiskAssessment> riskAssessments = riskAssessmentMapper.selectList(queryWrapper);
        return riskAssessments.stream().map(riskAssessment -> {
            RiskAssessmentDto dto = new RiskAssessmentDto();
            BeanUtils.copyProperties(riskAssessment, dto);
            setRiskLevelName(dto);
            return dto;
        }).collect(Collectors.toList());
    }

    @Override
    public void calculateRiskAssessmentForFund(String fundCode) {
        // 这里应该实现基于基金历史数据计算风险指标的逻辑
        // 简化实现，实际应该查询基金的历史净值数据并计算波动率、最大回撤、夏普比率等指标
    }

    @Override
    public void calculateRiskAssessmentForPortfolio(Long portfolioId) {
        // 这里应该实现基于投资组合中各基金的风险指标计算组合整体风险的逻辑
        // 简化实现，实际应该查询组合中各基金的风险指标并计算组合的整体风险水平
    }

    private void setRiskLevelName(RiskAssessmentDto dto) {
        if ("1".equals(dto.getRiskLevel())) {
            dto.setRiskLevelName("低风险");
        } else if ("2".equals(dto.getRiskLevel())) {
            dto.setRiskLevelName("中低风险");
        } else if ("3".equals(dto.getRiskLevel())) {
            dto.setRiskLevelName("中风险");
        } else if ("4".equals(dto.getRiskLevel())) {
            dto.setRiskLevelName("中高风险");
        } else if ("5".equals(dto.getRiskLevel())) {
            dto.setRiskLevelName("高风险");
        }
    }
}